"""
定义一些辅助函数
"""
import cv2
import numpy as np

def draw_line(img,line,color,thickness=2):
    """在图像中画线，以及其法方向"""
    nHeight,nWidth=img.shape[:2]
    ##计算两个端点
    a,b,c=line
    if line[0]==0:  ##与x轴平行的线
        x1=0; y1=-c/b
        x2=nWidth-1; y2=y1

    elif line[1]==0: ##与y轴平行的线
        x1=-c/a; y1=0
        x2=-c/a; y2=nHeight-1
    else:
        x1=0; y1=-c/b
        x2=nWidth-1; y2=-(c+a*x2)/b
        if y1<0 or y2>=nHeight:
            y1=0; x1=-c/a
            y2=nHeight-1; x2=-(c+b*y2)/a
    ##计算中点
    x1=int(x1); y1=int(y1); x2=int(x2); y2=int(y2)
    mx=x1+int((x2-x1)/2); my=y1+int((y2-y1)/2)
    ##单位化法方向
    dir=np.array([line[0],line[1]])
    dir=np.int32(15*dir/np.linalg.norm(dir))
    cv2.line(img,(x1,y1),(x2,y2),color,thickness)
    cv2.line(img,(mx,my),(mx+dir[0],my+dir[1]),color,thickness)
    #cv2.circle(img,(mx,my),5,color,thickness)

    return img

def rand_color():
    colors=(
        (88,87,86),
        (128,138,135),
        (252,230,202),
        (255,0,0),
        (156,102,31),
        (255,99,71),
        (255,0,255),
        (0,255,0),
        (0,255,255),
        (0,0,255),
        (255,255,0),
        (255,128,0),
        (128,42,42),
        (138,43,226)
           )
    n=len(colors)
    x=np.random.randint(0,n);

    return colors[x]